﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FineUICore;
using Microsoft.AspNetCore.Mvc;
using MyAppBox.Controllers;
using Newtonsoft.Json.Linq;
using SysFrame.BP;
using SysFrame.Message;

namespace MyAppBox.Areas.system.Controllers
{
    [Area("system")]
    public class system4Controller : SysPageController
    {
        // GET: system/system4
        public ActionResult Index()
        {
            InitData();
            return View();
        }

        //修改页面
        public ActionResult sys5Edit(string id)
        {
            Initsys5EditData(id);
            return View();
        }

        //初始化修改页面
        private void Initsys5EditData(string id)
        {
            Mssystem_5 ms5 = new Mssystem_5();
            if (id != null)
            {
                ms5 = new BLLsystem_5().Getsystem_5MsByKey(id);
            }
            else
            {
                ms5.System_5_10 = 0;
                ms5.System_5_20 = new BLLsystem_5().GetMaxTypeCode();

            }
            ViewBag.id = id;
            ViewBag.data = JObject.FromObject(ms5).ToString();
        }

        //修改页面
        public ActionResult sys4Edit(string id, string sys5id)
        {
            return View();
        }

        //初始化列表页面
        private void InitData()
        {
            List<Mssystem_5> dt = GetSys5Data();
            ViewBag.sys5GridData = dt;
        }

        //得到数据
        private List<Mssystem_5> GetSys5Data()
        {
            MsQuery ms = new MsQuery();
            ViewBag.btnhidehedden = false;
            //条件
            if (true)
            {
                ms.strquery += " and 1=1 ";
                Mssystem_1 lodeuser = GetSession<Mssystem_1>("LoginUser");
                if (lodeuser != null && !lodeuser.isadmin)
                {
                    ms.strquery += " and (SYSTEM_5_60='1' or SYSTEM_5_60 is Null )";
                    ViewBag.btnhidehedden = true;
                }
            }
            //ms.strorder = "  ";//排序语句
            //ms.strorder = "  ";//排序语句
            List<Mssystem_5> listms = new BLLsystem_5().Getsystem_5MsByQuery(ms);
            return listms;
            //DataTable dt = ((DataSet)new BLLsystem_5().Getsystem_5ByQuery(ms).data).Tables[0];
            //return dt;
        }

        /// <summary>
        /// 字典分类列表行点击事件
        /// </summary>
        /// <param name="rowid"></param>
        /// <param name="fields"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult sys5Grid_RowClick(string rowid, JArray fields)
        {
            //FineUIEx.ShowNotify("点击"+ rowid);
            MsQuery ms = new MsQuery();
            //条件
            if (true)
            {
                ms.strquery += " and 1=1 ";
                ms.strquery += " and SYSTEM_4_30='" + rowid + "' ";
            }
            //ms.strorder = "  ";//排序语句
            List<Mssystem_4> listms = new BLLsystem_4().Getsystem_4MsByQuery(ms);
            var sys4Grid = FineUICore.UIHelper.Grid("sys4Grid");
            sys4Grid.DataSource(listms.ToArray(), fields);
            return FineUICore.UIHelper.Result();
        }

        /// <summary>
        /// 字典列表点击事件
        /// </summary>
        /// <param name="rowid"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult sys4Grid_RowClick(string rowid)
        {
            Mssystem_4 listms = new BLLsystem_4().Getsystem_4MsByKey(rowid);
            var EditForm = FineUICore.UIHelper.Form("EditForm");
            EditForm.SetData(listms);
            return FineUICore.UIHelper.Result();
        }

        /// <summary>
        /// 切换显示
        /// </summary>
        /// <param name="rowid"></param>
        /// <param name="fields"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult showorhide(JArray rowid, JArray fields)
        {
            MsQuery msq = new MsQuery();
            msq.strquery += " and SYSTEM_5_10 in ('" + string.Join("','", rowid) + "')";
            List<Mssystem_5> listms = new BLLsystem_5().Getsystem_5MsByQuery(msq);
            foreach (var item in listms)
            {
                item.System_5_60 = item.System_5_60 == "1" ? "0" : "1";
                new BLLsystem_5().Savesystem_5(item);
            }
            List<Mssystem_5> dt = GetSys5Data();
            var sys5Grid = FineUICore.UIHelper.Grid("sys5Grid");
            sys5Grid.DataSource(dt, fields);
            return FineUICore.UIHelper.Result();
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="ms"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult EditFormsave(JObject ms, JArray fields)
        {
            Mssystem_4 ms1 = Mssystem_4.parse(ms);
            MsReturned rt = new BLLsystem_4().Savesystem_4(ms1);
            if (Convert.ToInt32(rt.strMsId) >= 0)
            {
                MsQuery msq = new MsQuery();
                //条件
                if (true)
                {
                    msq.strquery += " and 1=1 ";
                    msq.strquery += " and SYSTEM_4_30='" + ms1.System_4_30 + "' ";
                }
                //ms.strorder = "  ";//排序语句
                List<Mssystem_4> listms = new BLLsystem_4().Getsystem_4MsByQuery(msq);
                var sys4Grid = FineUICore.UIHelper.Grid("sys4Grid");
                sys4Grid.DataSource(listms.ToArray(), fields);
                ShowNotify("保存成功");
            }
            return FineUICore.UIHelper.Result();
        }

        /// <summary>
        /// 修改页面保存
        /// </summary>
        /// <param name="ms"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult EditSys5Formsave(JObject ms, string type)
        {
            Mssystem_5 ms1 = Mssystem_5.parse(ms);
            ms1.System_5_60 = "1";
            MsReturned rt = new BLLsystem_5().Savesystem_5(ms1);
            if (Convert.ToInt32(rt.strMsId) >= 0)
            {
                //注意不是追加 是先加
                FineUICore.PageContext.RegisterStartupScript("savecallback('" + rt.strMsId + "','" + type + "');");
                ShowNotify("保存成功");
            }
            return FineUICore.UIHelper.Result();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <param name="fields"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult sys5GridDel_Click(JArray id, JArray fields)
        {
            MsQuery msq = new MsQuery();
            msq.strquery = " and SYSTEM_5_10 in ('" + string.Join("','", id) + "')";
            MsReturned rt = new BLLsystem_5().Deletesystem_5ByQuery(msq);
            if (Convert.ToInt32(rt.strMsId) >= 0)
            {
                ShowNotify("保存成功");
                showorhide(new JArray(0), fields);
            }
            return FineUICore.UIHelper.Result();
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult sys4Gridnew_Click(string rowid)
        {
            Mssystem_4 listms = new Mssystem_4();
            listms.System_4_20 = new BLLsystem_4().GetMaxByTypeCode(rowid);
            listms.System_4_30 = rowid;
            listms.System_4_40 = "";
            listms.System_4_50 = "";
            listms.System_4_60 = "";
            listms.System_4_80 = "";
            var EditForm = FineUICore.UIHelper.Form("EditForm");
            EditForm.SetData(listms);
            return FineUICore.UIHelper.Result();
        }
    }
}